package com.test.mywebtest.controller;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.test.mywebtest.config.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@org.springframework.web.bind.annotation.RestController
@RequestMapping("/r")
public class RestController {


    @Autowired
    private RestTemplate restTemplate;

    private final String u  = "http://192.168.81.173:19535/document/carsoa-document";
    private final String yunyiqianDocument = "http://192.168.81.173:19535/document/carsoa-document";

    private final ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping(value = "rest", produces = {"application/json; charset=utf-8"})
    public void rest(){
        //
        //String s = "/document/v1/selectDocumentRFileList";
        String token = "3f9da4977a9e42909baec3321119c3ac";
        //
        //
        //// 构建请求体
        //MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        //body.add("documentId", "7c38b3a90adb4765a3ab2090408d1adb");
        //body.add("fileCategorys", "");
        //
        //
        //R rest = rest(u+s,token);
        //System.out.println(rest);

        // 构建请求体
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("documentId", "7c38b3a90adb4765a3ab2090408d1adb");
        body.add("fileId", "123");
        body.add("serviceTable", "oa_gsfw");
        body.add("fileCategory", "seal");
        body.add("sortNumber", "1");
        body.add("createBy", "杨帆");
        body.add("fileName", "");
        body.add("fileType", "");

        R r = restPost(yunyiqianDocument + "/document/v1/saveDocumnetRFile", body, token);
        System.out.println(r);

    }


    private R rest(String url, String token){
        // 设置请求头（可选）
        HttpHeaders headers = new HttpHeaders();
        headers.set("token", token); // 如果需要认证

        // 创建 HttpEntity 对象（可选）
        HttpEntity<String> entity = new HttpEntity<>(headers);

        // 发送 GET 请求并接收响应
        ResponseEntity<R> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                entity,
                R.class
        );

        // 处理响应
        if (response.getStatusCode().is2xxSuccessful()) {
            System.out.println(response.getBody());
        } else {
            System.out.println("Request failed with status code: " + response.getStatusCodeValue());
        }
        return response.getBody();
    }


    private R restPost(String url,MultiValueMap body, String token){
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.set("token", token); // 如果需要认证

        // 创建 HttpEntity 对象
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

        // 发送 POST 请求并接收响应
        ResponseEntity<R> response = restTemplate.postForEntity(url, requestEntity, R.class);

        // 处理响应
        if (response.getStatusCode().is2xxSuccessful()) {
            System.out.println("Response Body: " + response.getBody());
        } else {
            System.out.println("Request failed with status code: " + response.getStatusCodeValue());
        }
        return response.getBody();
    }
}
